﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;

namespace Login.Models
{
    /// <summary>
    /// The PasswordHasher class is used to hash a string using
    /// the SHA256 algorithm.
    /// </summary>
    public class PasswordHasher
    {
        /// <summary>
        /// Default contructor.
        /// </summary>
        public PasswordHasher()
        {

        }

        /// <summary>
        /// Hash a string using SHA256.
        /// </summary>
        /// <param name="textToHash">The text to hash</param>
        /// <returns>The SHA256 hash</returns>
        public string Hash(string textToHash)
        {
            /* create HashAlgorithm */
            HashAlgorithm algorithm = new SHA256CryptoServiceProvider();

            /* convert the text to bytes and hash the bytes */
            Byte[] textBytes = Encoding.UTF8.GetBytes(textToHash);
            Byte[] hashBytes = algorithm.ComputeHash(textBytes);

            /* convert the hash back to a string */
            string hash = BitConverter.ToString(hashBytes);

            /* format the string (make lowercase and remove dashes) */
            hash = hash.ToLower();
            hash = hash.Replace("-", "");

            /* return the hash */
            return hash;
        }
    }
}